﻿//-----------------------------------------------------------------------
// <copyright file="TransitionResultAdapter.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow
{
    using System.Web.Mvc;
    using System.Web.Routing;
    using Rosieks.Web.Flow.Internals;
    using Rosieks.Web.Flow.Metadata;

    public class TransitionResultAdapter : WebflowElementResultAdapterBase<Transition>
    {
        public override ActionResult CreateResult(Transition element, ControllerContext context)
        {
            var route = element.To.RouteData;

            if (route == null)
            {
                return new RedirectToRouteResult(new RouteValueDictionary()).OrAjaxEquivalent(context);
            }
            else if (!string.IsNullOrWhiteSpace(route.RouteName))
            {
                return new RedirectToRouteResult(route.RouteName, route.Values).OrAjaxEquivalent(context);
            }
            else
            {
                return new RedirectResult(route.Url).OrAjaxEquivalent(context);
            }
        }
    }
}
